package com.example.spark.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spark.dao.DirectionDao;
import com.example.spark.entity.Direction;
import com.example.spark.entity.User;
import com.example.spark.entity.result.CommonResult;
import com.example.spark.service.DirectionService;
import com.example.spark.service.minio.MinioCloudStorageService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.baomidou.mybatisplus.core.toolkit.IdWorker.getId;

@Service
public class DirectionServiceImpl extends ServiceImpl<DirectionDao, Direction> implements DirectionService {

    @Autowired
    private DirectionDao directionDao;
    @Resource
    MinioCloudStorageService minioCloudStorageService;

    //根据id查询学习方向
    @Override
    public CommonResult selectById(Long id) {
        Direction direction = directionDao.selectById(id);
        if (direction != null) {
            return new CommonResult<>(200, "查询成功", direction);
        }
        return new CommonResult<>(200, "查询失败");

    }

    //新增学习方向
    @Override
    public CommonResult addDirection(Direction direction) {
        long id = getId();
        direction.setId(id);
        String learningDirection = direction.getLearningDirection();
        Map<String, Object> map = new HashMap<>();
        map.put("learning_direction", learningDirection);
        List<Direction> directions = directionDao.selectByMap(map);
        if (directions == null || directions.isEmpty()) {
            int rowsAffected = directionDao.insert(direction);
            if (rowsAffected > 0) {
                return new CommonResult<>(200, "添加成功");
            } else {
                return new CommonResult<>(200, "添加失败");
            }
        }
        return new CommonResult<>(200, "添加失败");
    }

    //修改学习方向
    @Override
    public CommonResult updateDirection(Direction direction) {
        Long id = direction.getId();
        Direction direction1 = directionDao.selectById(id);
        if (direction1 != null) {
            directionDao.updateById(direction);
            return new CommonResult<>(200, "修改成功");
        }
        return new CommonResult<>(200, "修改失败");
    }

    //删除学习方向
    @Override
    public CommonResult deleteDirection(Long id) {
        Direction direction = directionDao.selectById(id);
        if (direction != null) {
            directionDao.deleteById(id);
            return new CommonResult<>(200, "删除成功");
        }
        return new CommonResult<>(200, "删除失败");
    }

    //列表
    @Override
    public IPage<Direction> getPage(int currentPage, int pageSize) {

        IPage<Direction> page = new Page<>(currentPage, pageSize);
        return directionDao.selectPage(page, null);
    }

    //图片处理
    @Override
    public CommonResult addPhoto(MultipartFile photoFile, Long id) throws IOException {

        String extension = FilenameUtils.getExtension(photoFile.getOriginalFilename());
        String url = minioCloudStorageService.uploadPhotoSuffix(photoFile.getBytes(), extension, String.valueOf(getId()));
        Direction direction = directionDao.selectById(id);
        direction.setPhoto(url);
        int is = directionDao.updateById(direction);
        if (is > 0) {
            return new CommonResult(200, "添加图片成功");
        }
        return new CommonResult(200, "添加图片失败");
    }

    //获取id和名称
    @Override
    public CommonResult getIdAndName() {
        Map<Long, String> map = new HashMap<>();
        List<Direction> directions = directionDao.selectList(null);

        if (Objects.isNull(directions)) {
            return new CommonResult<>(200, "尚未添加学习方向");
        }

        for (Direction direction : directions) {
            Long value = direction.getId();
            String label = direction.getLearningDirection();
            map.put(value, label);
        }
        return new CommonResult<>(200, "查询成功", map);
    }
}